package org.zk.dubbo_consumer.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.Admin;
import org.zk.entity.Catalog;
import org.zk.entity.Course;
import org.zk.entity.Lession;
import org.zk.service.AdminService;
import org.zk.service.CatalogService;
import org.zk.service.CourseService;
import org.zk.service.LessionService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;
    @Resource
    private LessionService lessionService;
    @Resource
    private CourseService courseService;
    @Resource
    private CatalogService catalogService;

    @RequestMapping("/getAllAdmin")
    public List<Admin> getAllAdmin() {
        return adminService.getAllAdmin();
    }


    @RequestMapping("/toLogin")
    public ModelAndView toLogin() {
        return  new ModelAndView("admin/login");
    }

    @RequestMapping("/adminLogin")
    public ModelAndView adminLogin() {
        return new ModelAndView("admin/index-2") ;
    }

    @RequestMapping("/toCourseList")
    public ModelAndView toCourseList() {
        return new ModelAndView("admin/courseList");
    }

    @RequestMapping("/toBroadcastList")
    public ModelAndView toBroadcastList(){
        return new ModelAndView("admin/broadcastList");
    }


    @RequestMapping("/toCourseListTeacher")
    public ModelAndView toCourseListTeacher() {
        return new ModelAndView("teacher/teacherconter/courseList");
    }

    @RequestMapping("/toList")
    public ModelAndView toList(int courseId) {
        ModelAndView modelAndView = new ModelAndView("admin/list");
        modelAndView.addObject("courseId", courseId);
        return modelAndView;
    }

    /**
     * 分页显示课节
     *
     * @param pageSize
     * @param pageNum
     * @param lession
     * @param state
     * @return
     */
    @RequestMapping("/getLessionList")
    public Map<String, Object> getList(int pageSize, int pageNum, Lession lession, String state,int courseId) {
        Map<String, Object> map = new HashMap<>();
        //通过
        if (state.equals("yes")) {
            lession.setLessionState("1");
        }
        //拒绝
        if (state.equals("refuse")) {
            lession.setLessionState("2");
        }
        //未审核
        if (state.equals("no")) {
            lession.setLessionState("0");
        }
        map.put("lession", lession);
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("courseId",courseId);
        PageInfo page = lessionService.getAllLessionPage(map);
        List<Lession> ls = page.getList();
        for (int i = 0; i < ls.size(); i++) {
            String flag = ls.get(i).getLessionState();
            if (flag.equals("0")) {
                ls.get(i).setLessionState("待审核");
            }
            if (flag.equals("1")) {
                ls.get(i).setLessionState("已通过");
            }
            if (flag.equals("2")) {
                ls.get(i).setLessionState("已拒绝");
            }
        }
        map.clear();
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }


    /**
     * 审核课程(同意)
     *
     * @return
     */
    @RequestMapping("/agree")
    @ResponseBody
    public List agreeLession(Lession lession, Course course) {
        List ls = new ArrayList();
        lession.setLessionState("1");
        int result = lessionService.updateLessionState(lession);
        //获取目录
        String mulu = catalogService.getStringCatalog(course.getCourseId());
        StringBuffer buffer = new StringBuffer(mulu);
        buffer.append(lession.getLessionId()).append(".").append(lession.getLessionTitle()).append("_");
        //目录追加
        Catalog catalog = new Catalog();
        catalog.setCourse(course);
        catalog.setCatalogLessionLise(buffer.toString());
        catalogService.updateCatalog(catalog);

        if (result > 0) {
            ls.add("succ");
            return ls;
        }
        return ls;
    }

    /**
     * 审核课程（拒绝）
     *
     * @param lession
     * @return
     */
    @RequestMapping("/refuse")
    public List refuseLession(Lession lession) {
        List ls = new ArrayList();
        lession.setLessionState("2");
        int result = lessionService.updateLessionState(lession);
        if (result > 0) {
            ls.add("succ");
            return ls;
        }
        return ls;
    }

    /**
     * 根据id查询课节
     *
     * @return
     */
    @RequestMapping("/getLessionById")
    public ModelAndView getLessionById(int lessionId) {
        Lession lession = lessionService.getLessionById(lessionId);
        ModelAndView modelAndView = new ModelAndView("admin/getLeById");
        modelAndView.addObject("lession", lession);
        return modelAndView;
    }


    /**
     * 分页显示所有课程
     *
     * @return
     */
    @RequestMapping("/getCourseList")
    public Map getCourseList(int pageSize, int pageNum, String state) {
        Map<String, Object> map = new HashMap<>();
        Course course = new Course();
        course.setCourseFlag(state);
        map.put("course", course);
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        PageInfo page = courseService.getAllCoursePage(map);
        List<Course> ls = page.getList();
        for (int i = 0; i < ls.size(); i++) {
            String name=ls.get(i).getCourseCover();
            ls.get(i).setCourseCover("/static/picture/" +name);
            String flag = ls.get(i).getCourseFlag();
            if (flag.equals("0")) {
                ls.get(i).setCourseFlag("待审核");
            }
            if (flag.equals("1")) {
                ls.get(i).setCourseFlag("已通过");
            }
            if (flag.equals("2")) {
                ls.get(i).setCourseFlag("拒绝");
            }
        }
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    /**
     * 审核course(同意)
     *
     * @return
     */
    @RequestMapping("/courseAgree")
    @ResponseBody
    public List agreeCourse(Course course) {
        System.out.println(course.getCourseName());
        List ls = new ArrayList();
        course.setCourseFlag("1");
        int result = courseService.updateFlag(course);
        if (result > 0) {
            //同意时为该course生成一天空的目录
            Catalog catalog = new Catalog();
            catalog.setCourse(course);
            catalog.setCatalogLessionLise("");
            catalogService.addCatalog(catalog);
            ls.add("succ");
            return ls;
        }
        return ls;
    }

    /**
     * 审核course（拒绝）
     *
     * @return
     */
    @RequestMapping("/courseRefuse")
    public List refuseCourse(Course course) {
        List ls = new ArrayList();
        course.setCourseFlag("2");
        int result = courseService.updateFlag(course);
        if (result > 0) {
            ls.add("succ");
            return ls;
        }
        return ls;
    }
    @RequestMapping("/yulan")
    public ModelAndView getVideo(int lessionId) {
        Lession lession = lessionService.getLessionById(lessionId);
        ModelAndView modelAndView = new ModelAndView("admin/queryVideo");
        modelAndView.addObject("lession", lession);
        return modelAndView;
    }

    @RequestMapping("/toType")
    public ModelAndView toType(){
        return new ModelAndView("admin/classify/type");
    }

    //跳转到发布通知界面
    @RequestMapping("/toNotice")
    public ModelAndView getLessionById() {
        ModelAndView modelAndView = new ModelAndView("admin/notice");
        return modelAndView;
    }























    public AdminService getAdminService() {
        return adminService;
    }

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    public LessionService getLessionService() {
        return lessionService;
    }

    public void setLessionService(LessionService lessionService) {
        this.lessionService = lessionService;
    }

    public CourseService getCourseService() {
        return courseService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }
}

